/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.Timer;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import nemeanshop.NemeanShop;
import nemeanshop.Product;

/**
 *
 * @author havardaxelsen
 */
public class NTable extends JTable{
    
    private int x;
    private boolean test = false;
    private int y;
    private Component component;
    
    private Timer timer;
    int rovv;
    int oldrovv;
    int col;
    private NPopup npop;
    private MusLytter mlistener = new MusLytter();
    private NemeanShop ns;

    public NTable(AbstractTableModel tm, NemeanShop ns) {
        super(tm);
        this.ns = ns;
        this.npop = new NPopup(rovv, ns);
        ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
            test = true;
            npop.show(component, x - 60, 40 + (40*rovv));
      }
  };
        int delay = 500;
        timer = new Timer(delay, taskPerformer);
        if (tm instanceof NProductModel)
        {
            this.addMouseMotionListener(mlistener);
            this.addMouseListener((mlistener));
        }
        this.setSelectionBackground(Color.blue);
    }
    
    public int getRovv()
    {
        return rovv;
    }
    
    
    
    private class MusLytter implements MouseMotionListener, MouseListener 
    {

        @Override
        public void mouseDragged(MouseEvent e)
        {
        }

        @Override
        public void mouseMoved(MouseEvent e)
        {   
            JTable table = (JTable) e.getSource();
            table.setFocusable(false);
            rovv = table.rowAtPoint(e.getPoint());
            NProductModel npm = (NProductModel) table.getModel();
            Product p = npm.getRow(rovv);
            component = e.getComponent();
            x = e.getX();
            y = e.getY();
            timer.stop();
            table.repaint();
            if( test == false ){
                
                oldrovv = rovv;
                npop = new NPopup(rovv, ns, p);
                timer.restart();
           
            }
            else{
                npop.show(component, x - 60, 40 + (40*rovv));
                if(oldrovv!=rovv){
                    
                    timer.restart();
                    npop.setVisible(false);
                    test = false;
                }
           
                
                
            }
            
        }

        @Override
        public void mouseClicked(MouseEvent me) {
            
        }

        @Override
        public void mousePressed(MouseEvent me) {

        }

        @Override
        public void mouseReleased(MouseEvent me) {

        }

        @Override
        public void mouseEntered(MouseEvent me) {
            
            timer.start();           
        }

        @Override
        public void mouseExited(MouseEvent me) {
            
            timer.stop();
        }
        
        
        
    }



   

}
    
    

